package com.zh.rent.service.impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.zh.rent.dao.AlipayConfigDao;
import com.zh.rent.pojo.AlipayConfig;
import com.zh.rent.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;


@Service
@Transactional(isolation = Isolation.READ_COMMITTED)
public class AlipayServiceImpl implements AlipayService {
    @Autowired
    AlipayConfigDao alipayConfigDao;

    @ResponseBody
    @Override
    public String toAlipay(String code,String AccountType,Double BuyMoney,String Voucher,Integer InvitationDays,String te) throws Exception{
        //订单保存
        //省略业务代码
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String s=uuid.substring(0, 8);
        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no =s;
            //AlipayConfig alipayConfig = new AlipayConfig();
        AlipayConfig alipayConfig = alipayConfigDao.selAli();
        System.out.println("支付宝："+alipayConfig.toString());
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getGatewayUrl(),
                alipayConfig.getApp_id(), alipayConfig.getMerchant_private_key(),
                "json", alipayConfig.getCharset(),
                alipayConfig.getAlipay_public_key(), alipayConfig.getSign_type());


        //设置请求参数
        AlipayTradePrecreateRequest alipayRequest = new AlipayTradePrecreateRequest ();
        //这里设置支付后跳转的地址
        alipayRequest.setReturnUrl(alipayConfig.getReturn_url());
        alipayRequest.setNotifyUrl(alipayConfig.getNotify_url());


        //付款金额，必填
        String total_amount = String.valueOf(BuyMoney);
        //订单名称，必填
        String subject =AccountType+"账号"+te;
        //商品描述，可空
        String body =Voucher+"&"+code;

        // 该笔订单允许的最晚付款时间，逾期将关闭交易。取值范围：1m～15d。m-分钟，h-小时，d-天，1c-当天（1c-当天的情况下，无论交易何时创建，都在0点关闭）。 该参数数值不接受小数点， 如 1.5h，可转换为 90m。
        String timeout_express = "5m";
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
        model.setOutTradeNo(out_trade_no);//订单号，邀请码
        model.setSubject(subject);//订单名称，必填
        model.setTotalAmount(total_amount);  //付款金额，必填
        model.setBody(body);//商品描述，可空
        model.setTimeoutExpress(timeout_express);  // 该笔订单允许的最晚付款时间，逾期将关闭交易。
        //设置参数
        alipayRequest.setBizModel(model);
        //请求
        AlipayTradePrecreateResponse response = null;
            response=alipayClient.execute (alipayRequest);
            System.out.print("打印打印打印："+ response.getBody ());
        return response.getBody();
    }

    @Override
    public AlipayConfig selAli() {
        return alipayConfigDao.selAli();
    }

    @Override
    public int upAlipay(AlipayConfig alipayConfig) {
        return alipayConfigDao.upAlipay(alipayConfig);
    }
}
